package com.srunpass.algorithm;

/**
 * @author srunpass
 * @version Id: 岛屿最大面积, v 0.1 2025/3/24 09:25 srunpass Exp $
 */
public class 岛屿最大面积 {


    static boolean[][] visited;

    int m = 0;

    int n = 0;

    public int maxAreaOfIsland(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int maxArea = 0;
        visited = new boolean[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int dsf = dsf(grid, i, j);
                maxArea = Math.max(maxArea, dsf);
            }
        }
        return maxArea;

    }

    private int dsf(int[][] grid, int i, int j) {
        if (i < 0 || i >= m || j < 0 || j >= n) return 0;
        if (visited[i][j] || grid[i][j] == 0) {
            return 0;
        }
        int count = 1;
        visited[i][j] = true;
        count += dsf(grid, i + 1, j);
        count += dsf(grid, i - 1, j);
        count += dsf(grid, i, j + 1);
        count += dsf(grid, i, j - 1);
        return count;

    }
}
